package net.wantongtech.xa.uploadbasicinfo.dao;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.wtkj.constant.NewClearingConstant;
import com.wtkj.vo.dzzf.base.dzzf.MultiDataSourceTemplate;
import com.wtkj.vo.dzzf.base.qxsjz.entity.basic.TollProvince;
import com.wtkj.zwxt.tool.ParticipantTool;
import net.wantongtech.xa.uploadbasicinfo.util.ClearingCommonUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.Map;

/**
 * 加载收费公路基础信息数据源实现
 *
 * @author MaShibo
 * 日期 :  2019/11/29
 */
@Repository
public class LoadTollBasicInfoDao extends MultiDataSourceTemplate {


    /**
     * 查询清分方/服务方编码信息
     *
     * @return 请分方/服务方编码信息
     */
    public ImmutableMap<String, TollProvince> queryTollProvinceIdMap() {
        final String sql = "SELECT t.tollprovinceid, t.name, t.province FROM xxjh_tollprovince t";
        return jdbcTemplate.query(sql, rs -> {
            ImmutableMap.Builder<String, TollProvince> builder = ImmutableMap.builder();
            BeanPropertyRowMapper<TollProvince> rowMapper = BeanPropertyRowMapper.newInstance(TollProvince.class);
            int rowNum = 1;
            while (rs.next()) {
                TollProvince tollProvince = rowMapper.mapRow(rs, rowNum);
                builder.put(tollProvince.getTollProvinceId(), tollProvince);
                rowNum++;
            }
            return builder.build();
        });
    }


    /**
     * 查询收费站ID -> 名称 信息
     *
     * @return 收费站信息MAP
     */
    public Map<String, String> queryStationMap() {
        final String sql = "SELECT t.id, t.name FROM xxjh_tollbasicinfo t WHERE t.type = ?";
        return jdbcTemplate.query(sql, rs -> {
            Map<String, String> map = Maps.newConcurrentMap();
            while (rs.next()) {
                map.put(rs.getString(1), rs.getString(2));
            }
            return map;
        }, NewClearingConstant.TollBasicType.STATION);
    }


    /**
     * 查询6107号段与银行自发渠道编号的对应关系
     *
     * @return 号段、渠道编号对应MAP
     */
    public ImmutableMap<String, String> querySectionNo2ChannelTypeMap() {
        final String sql =
                "SELECT a.czcardnum, a.jzcardnum, a.bankchanneltype AS channeltype\n" +
                        "  FROM xxjh_topclassissueragent a\n" +
                        " WHERE a.publishtype = 1\n" +
                        "   AND a.isinuse = 'Y'\n" +
                        "   AND a.bankchanneltype IS NOT NULL\n" +
                        "   AND a.issuerid = ?\n" +
                        "   AND a.subissuerid IS NOT NULL";
        return jdbcTemplate.query(sql, rs -> {
            ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
            while (rs.next()) {
                String channelType = rs.getString(3);
                builder.put(rs.getString(1), channelType);
                builder.put(rs.getString(2), channelType);
            }
            return builder.build();
        }, ParticipantTool.SX_QT_ISSUER);
    }


    /**
     * 查询6102 - 6106发行方与银行自发渠道编号的对应关系
     *
     * @return 发行方编号、渠道编号对应MAP
     */
    public ImmutableMap<String, String> queryIssuerId2ChannelTypeMap() {
        final String sql =
                "SELECT a.issuerid, a.bankchanneltype AS channeltype\n" +
                        "  FROM xxjh_topclassissueragent a\n" +
                        " WHERE a.publishtype = 1\n" +
                        "   AND a.isinuse = 'Y'\n" +
                        "   AND a.bankchanneltype IS NOT NULL\n" +
                        "   AND a.subissuerid IS NULL\n" +
                        " ORDER BY a.issuerid";
        return jdbcTemplate.query(sql, rs -> {
            ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
            while (rs.next()) {
                String issuerId = ClearingCommonUtils.convert2NewTransferNo(rs.getString(1));
                builder.put(issuerId, rs.getString(2));
            }
            return builder.build();
        });
    }


    /**
     * 查询6107渠道编码与子发行方编码的对应关系
     *
     * @return 渠道编码、子发行方编码对应MAP
     */
    public ImmutableMap<String, String> queryChannelType2SubIssuerIdMap() {
        final String sql =
                "SELECT a.bankchanneltype, a.subissuerid\n" +
                        "  FROM xxjh_topclassissueragent a\n" +
                        " WHERE a.isinuse = 'Y'\n" +
                        "   AND a.bankchanneltype IS NOT NULL\n" +
                        "   AND a.subissuerid IS NOT NULL\n" +
                        " ORDER BY a.subissuerid";
        return jdbcTemplate.query(sql, rs -> {
            ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
            while (rs.next()) {
                builder.put(rs.getString(1), rs.getString(2));
            }
            return builder.build();
        });
    }


}
